package gotlib

import (
	"fmt"
	"strings"
	"time"

	"gitee.com/tommickey/gose/v3/gose/gotconst"
	"gitee.com/tommickey/gose/v3/gose/gotinit"
)

/* GotLog 未完成 */

// LogE gotLog简单打印错误日志
func LogE(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogError {
		PrintRedLn(FormatLogString("[ERRO]", values...))
	}
}
func LogW(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogWarn {
		PrintYellowLn(FormatLogString("[WARN]", values...))
	}
}
func LogD(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogDebug {
		PrintBlueLn(FormatLogString("[DEBU]", values...))
	}
}
func LogDB(values ...interface{}) {
	PrintBlueLn(FormatLogString("[DEBU]", values...))
}

func LogDR(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogDebug {
		PrintRedLn(FormatLogString("[DEBU]", values...))
	}
}

func LogI(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogInfo {
		PrintNormalLn(FormatLogString("[INFO]", values...))
	}
}
func LogIR(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogInfo {
		PrintRedLn(FormatLogString("[INFO]", values...))
	}
}
func LogIG(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogInfo {
		PrintGreenLn(FormatLogString("[INFO]", values...))
	}
}
func LogIB(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogInfo {
		PrintBlueLn(FormatLogString("[INFO]", values...))
	}
}
func LogIY(values ...interface{}) {
	if gotinit.LogLevel >= gotconst.LogInfo {
		PrintYellowLn(FormatLogString("[INFO]", values...))
	}
}

func FormatLogString(LogType string, values ...interface{}) string {
	preTime := time.Now().Format("2006-01-02 15:04:05.000")
	var tempStr string
	for _, v := range values {
		tempStr = strings.Join([]string{tempStr, String(v)}, " ")
	}
	if len(tempStr) > 0 {
		tempStr = tempStr[1:]
	}
	tempStr = strings.Join([]string{preTime, LogType, tempStr}, " ")
	ConsoleLineMaxChar := 10000
	if len(tempStr) > ConsoleLineMaxChar {
		tempStr = strings.Join([]string{tempStr[0:ConsoleLineMaxChar], "...(", String(ConsoleLineMaxChar), "/", String(len(tempStr)), ") "}, "")
	}
	return tempStr
}

func ErrorAddInfo(err error, formatString string, args ...interface{}) error {
	if err == nil {
		return nil
	}
	return fmt.Errorf(formatString+" %w", args, err)
}
